﻿using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace Customs.Components.SLXNK
{
	/// <summary>
	/// Đơn vị Hải quan.
	/// </summary>
	public class HaiQuan
	{
		public static DataTable SelectAll(string maHaiQuan)
		{
			string connectionString = ConfigurationManager.AppSettings["SLXNKConnectionString"];
			string query = "SELECT Ma_HQ AS Ma, Ten_HQ AS Ten FROM SHAIQUAN WHERE MA_HQ LIKE @Ma_HQ ORDER BY Ten_HQ;";
			SqlParameter[] parameters = new SqlParameter[1];
			parameters[0] = new SqlParameter("@Ma_HQ", maHaiQuan);
			DataTable dt = SqlHelper.ExecuteDataset(connectionString, CommandType.Text, query, parameters).Tables[0];
			foreach (DataRow r in dt.Rows)
			{
			    r["Ma"] = r["Ma"].ToString().Trim();
                r["Ten"] = FontConverter.TCVN2Unicode(r["Ten"].ToString());
			}

			DataRow row = dt.NewRow();
			row["Ma"] = "   ";
			row["Ten"] = "[Tất cả]";
			dt.Rows.Add(row);
			return dt;
		}		

		public static DataTable SelectChiCucHaiQuanDaNang()
		{
			string connectionString = ConfigurationManager.AppSettings["SLXNKConnectionString"];
			string query = "SELECT Ma_HQ AS [Ma], Ten_HQ AS [Ten] FROM SHAIQUAN WHERE 1=2";
			DataTable dt = SqlHelper.ExecuteDataset(connectionString, CommandType.Text, query).Tables[0];

			DataRow row = dt.NewRow();

			row["Ma"] = "C34C";
			row["Ten"] = "Chi cục HQ CK Cảng Đà Nẵng KV I";
			dt.Rows.Add(row);
			
			row = dt.NewRow();
			row["Ma"] = "C34E";
			row["Ten"] = "Chi cục HQ CK Cảng Đà Nẵng KV II";
			dt.Rows.Add(row);
			
			row = dt.NewRow();
			row["Ma"] = "N34H";
			row["Ten"] = "Chi cục HQ KCN Đà Nẵng";
			dt.Rows.Add(row);
			
			row = dt.NewRow();
			row["Ma"] = "N34G";
			row["Ten"] = "Chi cục HQ KCN Hòa Khánh - Liên Chiểu Đà Nẵng";
			dt.Rows.Add(row);

			row = dt.NewRow();
			row["Ma"] = "A34B";
			row["Ten"] = "Chi cục HQ Sân bay QT Đà Nẵng";
			dt.Rows.Add(row);

			return dt;
		}		

		public static string GetTenHaiQuan(string ma)
		{
			string connectionString = ConfigurationManager.AppSettings["SLXNKConnectionString"];
			string query;
			query = "SELECT Ma_HQ AS Ma, Ten_HQ AS Ten FROM SHAIQUAN WHERE MA_HQ = @MaHaiQuan";	
			SqlParameter[] parameters = new SqlParameter[1];
			parameters[0] = new SqlParameter("@MaHaiQuan", SqlDbType.Char, 6);
			parameters[0].Value = ma;
			SqlDataReader reader = SqlHelper.ExecuteReader(connectionString, CommandType.Text, query, parameters);
			if (reader.Read())
			{
				return FontConverter.TCVN2Unicode(reader["Ten"].ToString());
			}
			return ma;
		}
		
		
	}
}
